import { _decorator, AudioSource, Button, Component, EditBox, game, Label, Node, Sprite } from 'cc';
import { GlobalVar } from '../GlobalVar';
import { GameEvent, WssEvent } from '../EventName';
const { ccclass, property } = _decorator;

@ccclass('AdventureAuction')
export class AdventureAuction extends Component {
    @property(Node)
    paimaiNode: Node = null;
    @property(Node)
    jingdezheNode: Node = null;
    @property(Label)
    paimainum: Label = null
    @property(Label)
    coinnum: Label = null;
    @property(Label)
    numLabel: Label = null;
    @property(Label)
    timeLabel: Label = null;
    @property(EditBox)
    nowPriceLabel: EditBox = null;
    @property(Button)
    btnBid: Button = null;
    @property(Label)
    nameLabel: Label = null;
    @property(Label)
    spendLabel: Label = null;
    @property(Node)
    btn_off: Node = null;
    @property(Node)
    tipPanel: Node = null;
    @property(AudioSource)
    audio1: AudioSource = null;

    private nowPrice: number = 0;

    protected onLoad(): void {
        game.on(WssEvent.ON_ADVENTURE_AUCTION_REWARD, this.ON_ADVENTURE_AUCTION_REWARD, this);
    }
    protected onDestroy(): void {
        game.off(WssEvent.ON_ADVENTURE_AUCTION_REWARD, this.ON_ADVENTURE_AUCTION_REWARD, this);
    }

    init(data: any) {
        this.paimaiNode.active = true;
        this.jingdezheNode.active = false;
        console.log(data.body.diamondCoin.value, data.body.diamondCoin, data.body, "-------------------------")
        this.paimainum.string = data.body.diamondCoin.value + "CT";
        this.coinnum.string = data.body.startPrice;
        this.nowPrice = data.body.startPrice;
        this.numLabel.string = data.body.headcount;
        this.timeLabel.string = String((30 - data.body.headcount));
        this.nowPriceLabel.string = String(this.nowPrice);
        this.btnBid.interactable = true;
        this.btnBid.node.getComponent(Sprite).grayscale = false;
        this.btn_off.active = false;
    }
    updateTime(data: any) {
        let nowTime = Date.now();
        let time = data.body.initCountdown - Math.floor((nowTime - data.body.timestamp) / 1000)
        time = time < 0 ? 0 : time;
        this.timeLabel.string = String(time);
        this.numLabel.string = data.body.headcount;
    }
    onTouchAdd() {
        this.nowPrice += 10000;
        this.nowPriceLabel.string = String(this.nowPrice);
    }

    editorChange() {
        if (Number(this.nowPriceLabel.string) < 10000) {
            this.nowPriceLabel.string = "10000";
        }
        this.nowPrice = Number(this.nowPriceLabel.string)
    }
    openTipPanel() {
        this.tipPanel.active = !this.tipPanel.active
    }

    onTouchBid() {
        let nowCoin = Number(GlobalVar.userInfo.walletList[0].normalBalance);
        if (this.nowPrice > nowCoin) {
            game.emit(GameEvent.SHOW_TOAST, "Insufficient gold coins");
            return;
        }
        let data = {
            cmd: 10,
            body: {
                amount: this.nowPrice
            }
        }
        GlobalVar.wssAD.sendBinaryMessage(data)
        game.emit(GameEvent.SHOW_TOAST, "Bid successful");
        this.btnBid.interactable = false;
        this.btnBid.node.getComponent(Sprite).grayscale = true;
    }
    ON_ADVENTURE_AUCTION_REWARD(data?: any) {
        this.paimaiNode.active = false;
        this.jingdezheNode.active = true;
        this.audio1.play();
        this.nameLabel.string = data.body.username;
        this.spendLabel.string = data.body.spendGold.value;
        this.btn_off.active = true;
    }
}


